
package edu.tongji.jiaowu.dao.jdbc;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import edu.tongji.jiaowu.dao.ParameterDao;
import com.tongji.quickCRUDQ.define.Row;
import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.dao.jdbc.BaseDaoJdbc;
import com.tongji.quickCRUDQ.exception.FormatException;

public class ParameterDaoJdbc extends BaseDaoJdbc implements ParameterDao {
	String tableName="T_PARAMETER";
	String beanName="parameter";
	
	public CommonBean createParameter(){
		CommonBean parameters=this.createBean(tableName,beanName);
                return parameters;		
	}
	
/*
	public CommonBean findParameters(String refid) {
		CommonBean parameters=this.createParameter();
		String sql="select * from T_PARAMETER where refid=?";
		List data=this.getCommonDao().select(sql,new String[]{refid});
		parameters.addData(data);
                return parameters;
       }
*/
	public CommonBean findParameterById(String id) {
		CommonBean parameters=this.createParameter();
		String sql="select * from T_PARAMETER where id=?";
		List data=this.getCommonDao().select(sql,new String[]{id});
		parameters.addData(data);
       return parameters;
  }
  
	public CommonBean findAllParameters() {
		CommonBean parameters=this.createParameter();
		String sql="select * from T_PARAMETER ";
		List data=this.getCommonDao().select(sql,new String[]{});
		parameters.addData(data);
    return parameters;
  }
	public void insertParameter(CommonBean parameterBean){
		List data=parameterBean.getData();
		Row row=this.getRow(tableName);
		this.getCommonDao().templateBatchInsert(row,data,this.getIdGenerator());
	}

	public CommonBean findParameterByName(String paramName) {
		CommonBean parameters=this.createParameter();
		String sql="select * from T_PARAMETER where name=?";
		List data=this.getCommonDao().select(sql,new String[]{paramName});
		parameters.addData(data);
       return parameters;
    }

	public String findParameterValue(String paramName) {
		CommonBean parameters=this.findParameterByName(paramName);
		if (parameters.getSize()==0) {
			throw new FormatException("无此参数:"+paramName);
		}
		return parameters.getPropertyAsString(0, "value1");
	}

	public CommonBean findParameters(String[] params) {
		if(params==null) return null;
		String[] values=new String[params.length]; 
		StringBuffer sql=new StringBuffer("select * from t_parameter where name in(");
		for (int i = 0; i < params.length; i++) {
			String param = params[i];
			sql.append("?");
			values[i]=param;
			if (i<params.length-1) {
				sql.append(",");
			}
		}
		sql.append(")");
		List data=getCommonDao().select(sql.toString(), values);
		CommonBean parameters=this.createParameter();
		parameters.addData(data);
		return parameters;
	}

	public void updateParameter(Map parameter) {
		List params=new ArrayList();
		Iterator iter=parameter.keySet().iterator();
		while (iter.hasNext()) {
			String paramName= (String) iter.next();
			String paramValue=(String) parameter.get(paramName);
			params.add(new String[]{paramValue,paramName});
		}
		String sql="update t_parameter set value1=? where name=?";
		getCommonDao().batchUpdateRecord(sql, params);
	}
}

